package Web.service.impl;

import Web.constants.SystemCanstants;
import Web.domain.ResponseResult;
import Web.domain.entity.Article;
import Web.domain.entity.Comment;
import Web.domain.entity.User;
import Web.enums.AppHttpCodeEnum;
import Web.exception.SystemException;
import Web.mapper.CommentMapper;
import Web.service.CommentService;
import Web.service.UserService;
import Web.utils.BeanCopyUtils;
import Web.vo.CommentVo;
import Web.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.util.SystemOutLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service("CommentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper,Comment> implements CommentService {
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    @Override
    public ResponseResult CommentList(Long ArticleID) {
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Comment::getArticleId, ArticleID);
        lambdaQueryWrapper.eq(Comment::getRootId, SystemCanstants.COMMENT_STATUS_ROOT);

//        Page<Comment> page = new Page<>(pageNum, pageSize);
//        page(page, lambdaQueryWrapper);
        List<Comment> comments=commentService.list(lambdaQueryWrapper);
        List<Comment> sortedComments = comments.stream()
                .sorted(Comparator.comparing(Comment::getCreateTime).reversed())
                .collect(Collectors.toList());
        List<CommentVo> commentVoList = xxToCommentList(sortedComments);

        for (CommentVo commentVo:commentVoList){
            List<CommentVo> children=getChildren(commentVo.getId());
            commentVo.setChildren(children);
        }
//        return ResponseResult.okResult(new PageVo(commentVoList, page.getTotal()));
        return ResponseResult.okResult(commentVoList);
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        if(!StringUtils.hasText(comment.getContent())){
            //AppHttpCodeEnum是我们写的枚举类，CONTENT_NOT_NULL代表提示''
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);

        return ResponseResult.okResult();
    }


    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId, id);
        //对子评论按照时间进行排序
        queryWrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        //调用下面那个方法
        List<CommentVo> commentVos = xxToCommentList(comments);
        return commentVos;
    }
    private List<CommentVo> xxToCommentList(List<Comment> list) {
        //获取评论区的所有评论
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        System.out.print(commentVos);
        //遍历。由于封装响应好的数据里面没有username字段，所以我们还不能返回给前端。这个遍历就是用来得到username字段
        for (CommentVo commentVo : commentVos) {
            //
            //需要根据commentVo类里面的createBy字段，然后用createBy字段去查询user表的nickname字段(子评论的用户昵称)
//            Long comment_create=commentVo.getCreateBy();
//            User user1=userService.getById(comment_create);
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            String avatar=userService.getById(commentVo.getCreateBy()).getAvatar();
            //然后把nickname字段(发这条子评论的用户昵称)的数据赋值给commentVo类的username字段
            commentVo.setUsername(nickName);
            commentVo.setAvatar(avatar);
            //查询根评论的用户昵称。怎么判断是根评论的用户呢，判断getToCommentUserId为1，就表示这条评论是根评论
            if (commentVo.getToCommentUserId() != -1) {
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                //然后把nickname字段(发这条根评论的用户昵称)的数据赋值给commentVo类的toCommentUserName字段
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVos;
    }
}